package com.wangsd.web.face.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.utils.DateUtils;
import com.wangsd.common.utils.ExcelData;
import com.wangsd.common.utils.POIUtil;
import com.wangsd.web.MyController;
import com.wangsd.web.face.entity.ReceiveRecord;
import com.wangsd.web.face.entity.TOrder;
import com.wangsd.web.face.service.TOrderService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-23
 */
@Api(value = "tOrder", tags = "订单/交易记录管理")
@RestController
@RequestMapping("/face/tOrder")
public class TOrderController extends MyController {

    @Autowired
    TOrderService orderService;

    @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @RequestMapping(value = "/selectList/{pageNum}/{pageSize}", method = RequestMethod.POST)
    public Result<IPage<TOrder>> selectList(@ApiParam(value = "当前页", required = true) @PathVariable("pageNum") Integer pageNum,
                                           @ApiParam(value = "每页大小", required = true) @PathVariable("pageSize") Integer pageSize,
                                           @RequestBody @ApiParam(name = "params", value = "传入json格式") TOrder params) {
        SysUser loginUser = getUserInfo();
        IPage<TOrder> resultIPage = orderService.selectList(pageNum, pageSize, params, loginUser.getCode());
        Map statistics = orderService.selectListStatistics(params,loginUser.getCode());
        Map map = new HashMap();
        map.put("pageInfo",resultIPage);
        map.put("statistics",statistics);
        return Result.success(map);
    }


    @ApiOperation(value = "通过id查询订单详情", notes = "通过id查询订单详情", httpMethod = "GET", response = Result.class)
    @GetMapping("/selectById/{id}")
    public Result<TOrder> selectById(@ApiParam(value = "订单ID", required = true) @PathVariable Long id) {
        TOrder result = orderService.getById(id);
        return Result.success(result);
    }

    @ApiOperation(value = "通过订单id查询分账详情", notes = "通过订单id查询分账详情", httpMethod = "GET", response = Result.class)
    @GetMapping("/selectPaymentByOrderId/{id}")
    public Result<TOrder> selectPaymentByOrderId(@ApiParam(value = "订单ID", required = true) @PathVariable Long id) {
        List<ReceiveRecord> list = orderService.selectPaymentByOrderId(id);
        return Result.success(list);
    }



    /**
     * 导出交易明细
     * @return
     */
    @ApiOperation(value = "导出交易明细", notes = "导出交易明细", httpMethod = "POST")
    @PostMapping("/exportExcel")
    public void exportExcel( @RequestBody @ApiParam(name = "params", value = "传入json格式") TOrder params) {
        SysUser loginUser = getUserInfo();
        String fileName =  DateUtils.formatDate(new Date(), "yyyyMMdd HH:mm:ss") + "-交易记录.xls";
        List<TOrder> list = orderService.selectList(params, loginUser.getCode());
        ExcelData excelData = new ExcelData();
        List<String> titles = new ArrayList<>();
        ArrayList<List<Object>> rowList = new ArrayList<>();//行
        titles.add("订单时间");
        titles.add("商户名称");
        titles.add("门店名称");
        titles.add("商户订单号");
        titles.add("交易订单号");
        titles.add("订单金额");
        titles.add("实收金额");
        titles.add("实付金额");
        titles.add("优惠金额");
        titles.add("交易状态");
        titles.add("支付方式");
        titles.add("店员名称");
        titles.add("退款人");
        excelData.setTitles(titles);
        for (TOrder order : list) {
            List<Object> strList = new ArrayList<>();
            strList.add(DateUtils.formatDate(order.getCreatedTime(), "yyyy-MM-dd HH:mm:ss"));
            strList.add(order.getMerchantName());
            strList.add(order.getStoreName());
            strList.add(order.getOutTradeNo());
            strList.add(order.getTransactionId());
            strList.add(this.fenToYuan(order.getMemberTotalFee()));
            strList.add(this.fenToYuan(order.getSettlementTotalFee()));
            strList.add(this.fenToYuan(order.getRealPayFee()));
            strList.add(this.fenToYuan(order.getCouponFee()));
            //订单状态
            switch (order.getOrderStatus()) {
                case 0:
                    strList.add("未支付");
                    break;
                case 1:
                    strList.add("已支付");
                    break;
                case 2:
                    strList.add("支付失败");
                    break;
                case 3:
                    strList.add("已退款");
                    break;
                case 4:
                    strList.add("已撤销");
                    break;
                case 5:
                    strList.add("押金支付");
                    break;
            }

            //支付方式
            switch (order.getPayType()) {
                case 1:
                    strList.add("微信");
                    break;
                case 2:
                    strList.add("支付宝");
                    break;
                case 3:
                    strList.add("会员支付");
                    break;
                case 4:
                    strList.add("通用通");
                    break;
            }
            strList.add(StringUtils.isNotBlank(order.getOrderCreateUserName()) ? order.getOrderCreateUserName() : "");
            strList.add(StringUtils.isNotBlank(order.getOrderRefundUserName()) ? order.getOrderRefundUserName() : "");
            rowList.add(strList);
        }
        excelData.setRows(rowList);
        try {
            POIUtil.exportExcel(response, fileName, excelData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String fenToYuan(Long money){
        if(money == null ){
           return "0.00";
        }
        BigDecimal bg = new BigDecimal(money * 0.01);
        DecimalFormat df1 = new DecimalFormat("0.00");
        String fee = df1.format(bg);
        return fee;
    }
}

